/**************************************************************************//**
\file  sensors.h

\brief Implementation of sensors interface.

\author
    Atmel Corporation: http://www.atmel.com \n
    Support email: avr@atmel.com

  Copyright (c) 2008-2012, Atmel Corporation. All rights reserved.
  Licensed under Atmel's Limited License Agreement (BitCloudTM).

\internal
  History:
    12/05/08 A. Khromykh - Created
*******************************************************************************/
#if APP_DISABLE_BSP != 1

/******************************************************************************
                   Includes section
******************************************************************************/
#include <sensors.h>
#include <pwrCtrl.h>
#include <tmp175.h>
#include <bmp085.h>
#include <hih5031.h>
#include <uv.h>
#include <pv.h>
#include <winddir.h>
#include <windspeed.h>
#include <battery.h>
#include <bspadc.h>

/******************************************************************************
                   Implementations section
******************************************************************************/
/***************************************************************************//**
\brief Opens temperature sensor.

\return
  BC_FAIL - sensor has been already open. \n
  BC_SUCCESS - otherwise.
*******************************************************************************/
result_t BSP_OpenTemperatureSensor(void)
{
  result_t result;

  if (BC_SUCCESS == (result = openTmp175()))
  {
    //bspOnPeriphery(SENSOR_TEMPERATURE);
  }
  return result;
}

/***************************************************************************//**
\brief Closes the temperature sensor.

\return
  BC_FAIL - if a hardware error has occured or
            there is uncompleted ReadData request. \n
  BC_SUCCESS - otherwise.
*******************************************************************************/
result_t BSP_CloseTemperatureSensor(void)
{
  result_t result;

  if (BC_SUCCESS == (result = closeTmp175()))
  {
    //bspOffPeriphery(SENSOR_TEMPERATURE);
  }
  return result;
}

/**************************************************************************//**
\brief Reads data from the temperature sensor.
\param[in]
    result - the result of the requested operation.
             true - operation finished successfully, false - some error has
             occured.
\param[in]
    data - sensor data.
\return
    BC_FAIL - previous request was not completed. \n
    BC_SUCCESS - otherwise.
******************************************************************************/
result_t BSP_ReadTemperatureData(void (*f)(bool result, int16_t data))
{
  return readTmp175Data(f);
}

/***************************************************************************//**
\brief Opens pressure sensor.

\return
  BC_FAIL - sensor has been already open. \n
  BC_SUCCESS - otherwise.
*******************************************************************************/
result_t BSP_OpenPressureSensor(void)
{
  result_t result;

  if (BC_SUCCESS == (result = openBmp085()))
  {
    //bspOnPeriphery(SENSOR_TEMPERATURE);
  }
  return result;
}

/***************************************************************************//**
\brief Closes the pressure sensor.

\return
  BC_FAIL - if a hardware error has occured or
            there is uncompleted ReadData request. \n
  BC_SUCCESS - otherwise.
*******************************************************************************/
result_t BSP_ClosePressureSensor(void)
{
  result_t result;

  if (BC_SUCCESS == (result = closeBmp085()))
  {
    //bspOffPeriphery(SENSOR_TEMPERATURE);
  }
  return result;
}

/**************************************************************************//**
\brief Reads data from the pressure sensor.
\param[in]
    result - the result of the requested operation.
             true - operation finished successfully, false - some error has
             occured.
\param[in]
    data - sensor data.
\return
    BC_FAIL - previous request was not completed. \n
    BC_SUCCESS - otherwise.
******************************************************************************/
result_t BSP_ReadPressureData(void (*f)(bool result, uint32_t data))
{
  return readBmp085Data(f);
}


/**************************************************************************//**
\brief Reads temperature from the pressure sensor.
\param[in]
    result - the result of the requested operation.
             true - operation finished successfully, false - some error has
             occurred.
\param[in]
    data - sensor data.
\return
    BC_FAIL - previous request was not completed. \n
    BC_SUCCESS - otherwise.
******************************************************************************/
result_t BSP_ReadPressureTemp(void (*f)(bool result, int16_t data))
{
  return readBmp085Temperature(f);
}

/***************************************************************************//**
\brief Opens humidity sensor.

\return
  BC_FAIL - sensor has been already open. \n
  BC_SUCCESS - otherwise.
*******************************************************************************/
result_t BSP_OpenHumiditySensor(void)
{
  result_t result;

  if (BC_SUCCESS == (result = openHih5031()))
  {
    //bspOnPeriphery(SENSOR_TEMPERATURE);
  }
  return result;
}

/***************************************************************************//**
\brief Closes the humidity sensor.

\return
  BC_FAIL - if a hardware error has occurred or
            there is uncompleted ReadData request. \n
  BC_SUCCESS - otherwise.
*******************************************************************************/
result_t BSP_CloseHumiditySensor(void)
{
  result_t result;

  if (BC_SUCCESS == (result = closeHih5031()))
  {
    //bspOffPeriphery(SENSOR_TEMPERATURE);
  }
  return result;
}

/**************************************************************************//**
\brief Reads data from the humidity sensor.
\param[in]
    result - the result of the requested operation.
             true - operation finished successfully, false - some error has
             occurred.
\param[in]
    data - sensor data.
\return
    BC_FAIL - previous request was not completed. \n
    BC_SUCCESS - otherwise.
******************************************************************************/
result_t BSP_ReadHumidityData(void (*f)(bool result, uint16_t data))
{
  return readHih5031Data(f);
}

/***************************************************************************//**
\brief Opens the UV sensor.
\return
  BC_FAIL - sensor has been already open. \n
  BC_SUCCESS - otherwise.
*******************************************************************************/
result_t BSP_OpenUVSensor(void)
{
  result_t result;

  if (BC_SUCCESS == (result = openUV()))
  {
    //bspOnPeriphery(SENSOR_LIGHT);
  }
  return result;
}

/***************************************************************************//**
\brief Closes the UV sensor.
\return
  BC_FAIL - if a hardware error has occured or
         there is uncompleted ReadData request. \n
  BC_SUCCESS  - otherwise.
*******************************************************************************/
result_t BSP_CloseUVSensor(void)
{
  result_t result;

  if (BC_SUCCESS == (result = closeUV()))
  {
    //bspOffPeriphery(SENSOR_LIGHT);
  }
  return result;
}

/**************************************************************************//**
\brief Reads data from the UV sensor.
\param[in]
    result - the result of the requested operation.
             true - operation finished successfully, false - some error has
             occurred.
\param[in]
    data - sensor data.
\return
    BC_FAIL - previous request was not completed. \n
    BC_SUCCESS - otherwise.
******************************************************************************/
result_t BSP_ReadUVData(void (*f)(bool result, uint16_t data))
{
  return readUVData(f);
}

/***************************************************************************//**
\brief Opens the PV sensor.
\return
  BC_FAIL - sensor has been already open. \n
  BC_SUCCESS - otherwise.
*******************************************************************************/
result_t BSP_OpenPVSensor(void)
{
  result_t result;

  if (BC_SUCCESS == (result = openPV()))
  {
    //bspOnPeriphery(SENSOR_LIGHT);
  }
  return result;
}

/***************************************************************************//**
\brief Closes the PV sensor.
\return
  BC_FAIL - if a hardware error has occurred or
         there is uncompleted ReadData request. \n
  BC_SUCCESS  - otherwise.
*******************************************************************************/
result_t BSP_ClosePVSensor(void)
{
  result_t result;

  if (BC_SUCCESS == (result = closePV()))
  {
    //bspOffPeriphery(SENSOR_LIGHT);
  }
  return result;
}

/**************************************************************************//**
\brief Reads data from the PV sensor.
\param[in]
    result - the result of the requested operation.
             true - operation finished successfully, false - some error has
             occurred.
\param[in]
    data - sensor data.
\return
    BC_FAIL - previous request was not completed. \n
    BC_SUCCESS - otherwise.
******************************************************************************/
result_t BSP_ReadPVData(void (*f)(bool result, uint16_t data))
{
  return readPVData(f);
}

/***************************************************************************//**
\brief Opens the Wind Direction sensor.
\return
  BC_FAIL - sensor has been already open. \n
  BC_SUCCESS - otherwise.
*******************************************************************************/
result_t BSP_OpenWindDirectionSensor(void)
{
  result_t result;

  if (BC_SUCCESS == (result = openWindDir()))
  {
    //bspOnPeriphery(SENSOR_LIGHT);
  }
  return result;
}

/***************************************************************************//**
\brief Closes the Wind Direction sensor.
\return
  BC_FAIL - if a hardware error has occurred or
         there is uncompleted ReadData request. \n
  BC_SUCCESS  - otherwise.
*******************************************************************************/
result_t BSP_CloseWindDirectionSensor(void)
{
  result_t result;

  if (BC_SUCCESS == (result = closeWindDir()))
  {
    //bspOffPeriphery(SENSOR_LIGHT);
  }
  return result;
}

/**************************************************************************//**
\brief Reads data from the Wind Direction sensor.
\param[in]
    result - the result of the requested operation.
             true - operation finished successfully, false - some error has
             occurred.
\param[in]
    data - sensor data.
\return
    BC_FAIL - previous request was not completed. \n
    BC_SUCCESS - otherwise.
******************************************************************************/
result_t BSP_ReadWindDirectionData(void (*f)(bool result, uint16_t data))
{
  return readWindDirData(f);
}

/***************************************************************************//**
\brief Opens the Wind Speed sensor.
\return
  BC_FAIL - sensor has been already open. \n
  BC_SUCCESS - otherwise.
*******************************************************************************/
result_t BSP_OpenWindSpeedSensor(void)
{
  result_t result;

  if (BC_SUCCESS == (result = openWindSpeed()))
  {
    //bspOnPeriphery(SENSOR_LIGHT);
  }
  return result;
}

/***************************************************************************//**
\brief Closes the Wind Speed sensor.
\return
  BC_FAIL - if a hardware error has occurred or
         there is uncompleted ReadData request. \n
  BC_SUCCESS  - otherwise.
*******************************************************************************/
result_t BSP_CloseWindSpeedSensor(void)
{
  result_t result;

  if (BC_SUCCESS == (result = closeWindSpeed()))
  {
    //bspOffPeriphery(SENSOR_LIGHT);
  }
  return result;
}

/**************************************************************************//**
\brief Reads data from the Wind Speed sensor.
\param[in]
    result - the result of the requested operation.
             true - operation finished successfully, false - some error has
             occurred.
\param[in]
    data - sensor data.
\return
    BC_FAIL - previous request was not completed. \n
    BC_SUCCESS - otherwise.
******************************************************************************/
result_t BSP_ReadWindSpeedData(void (*f)(uint16_t data))
{
  readWindSpeedData(f);
  return BC_SUCCESS;
}

/***************************************************************************//**
\brief Opens the battery sensor.
\return
  BC_FAIL - sensor has been already open. \n
  BC_SUCCESS - otherwise.
*******************************************************************************/
result_t BSP_OpenBatterySensor(void)
{
  result_t result;

  if (BC_SUCCESS == (result = openBattery()))
  {
    //bspOnPeriphery(SENSOR_BATTERY);
  }
  return result;
}

/***************************************************************************//**
\brief Closes the battery sensor.
\return
  BC_FAIL - sensor was not opened. \n
  BC_SUCCESS  - otherwise.
*******************************************************************************/
result_t BSP_CloseBatterySensor(void)
{
  result_t result;

  if (BC_SUCCESS == (result = closeBattery()))
  {
    //bspOffPeriphery(SENSOR_BATTERY);
  }
  return result;
}

/**************************************************************************//**
\brief Reads data from battery sensor.
\param[in]
    data - sensor data.
\return
    BC_FAIL - previous request was not completed, or sensor was not opened. \n
    BC_SUCCESS - otherwise.
******************************************************************************/
result_t BSP_ReadBatteryData(BspBatteryCb_t cb)
{
  return readBatteryData(cb);
}

/***************************************************************************//**
\brief Opens the Analog to Digital Converter.

\return
  BC_FAIL - sensor has been already open. \n
  BC_SUCCESS - otherwise.
*******************************************************************************/
result_t BSP_OpenAdc(void)
{
  result_t result;

  if (BC_SUCCESS == (result = openBspAdc()))
  {
    //bspOnPeriphery(SENSOR_TEMPERATURE);
  }
  return result;
}

/***************************************************************************//**
\brief Closes the Analog to Digital Converter.

\return
  BC_FAIL - if a hardware error has occurred or
            there is uncompleted ReadData request. \n
  BC_SUCCESS - otherwise.
*******************************************************************************/
result_t BSP_CloseAdc(void)
{
  result_t result;

  if (BC_SUCCESS == (result = closeBspAdc()))
  {
    //bspOffPeriphery(SENSOR_TEMPERATURE);
  }
  return result;
}

#endif // APP_DISABLE_BSP != 1

//end of sensors.c
